package com.cst.zju.anime.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.cst.zju.anime.model.Anime;
import com.cst.zju.anime.model.Classify;
import com.cst.zju.anime.model.Detail;
import com.cst.zju.anime.model.DetailWithBLOBs;
import com.cst.zju.anime.model.Tags;
import com.cst.zju.anime.service.IAnimeService;
import com.cst.zju.anime.service.IClassifyService;
import com.cst.zju.anime.service.IDetailService;
import com.cst.zju.anime.service.ITagsService;
import com.cst.zju.anime.utils.Page;


@Controller
public class AnimeController {
	
	@Autowired
	private IAnimeService iAnimeService;
	
	@Autowired
	private IDetailService iDetailService;
	
	@Autowired
	private IClassifyService iClassifyService;
	
	@Autowired
	private ITagsService iTagsService;
	
	
	/**
	 * 跳转到首页
	 * @return
	 */
	@RequestMapping({"/","index"})
	public ModelAndView index(){
		System.out.println("index!");
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("index");
		
		
		//字母分类
		List<Classify> allClassify = iClassifyService.getAllClassify();
		//新增连载动漫
		List<Anime> newSerialAnime = iAnimeService.getNewSerialAnime(0, 10);
		//新增完结动漫
		List<Anime> newCompletionAnime = iAnimeService.getNewCompletionAnime(0, 10);
		//推荐连载动漫
		List<Anime> recommendSerialAnime = iAnimeService.getRecommendSerialAnime(0, 10);
		//推荐完结动漫
		List<Anime> recommendCompletionAnime = iAnimeService.getRecommendCompletionAnime(0, 10);
		//一周更新
		List<Detail> detailListForWeek1 = iDetailService.getDetailListForWeek("星期一");
		List<Detail> detailListForWeek2 = iDetailService.getDetailListForWeek("星期二");
		List<Detail> detailListForWeek3 = iDetailService.getDetailListForWeek("星期三");
		List<Detail> detailListForWeek4 = iDetailService.getDetailListForWeek("星期四");
		List<Detail> detailListForWeek5 = iDetailService.getDetailListForWeek("星期五");
		List<Detail> detailListForWeek6 = iDetailService.getDetailListForWeek("星期六");
		List<Detail> detailListForWeek7 = iDetailService.getDetailListForWeek("星期日");
		//最新更新
		List<Detail> newAnime = iDetailService.getLatestList();
		
		//热门新番
		List<Anime> hotSeasonAnime_201507 = iAnimeService.getHotSeasonAnime(0,10,"2015年七月新番");
		List<Anime> hotSeasonAnime_201504 = iAnimeService.getHotSeasonAnime(0,10,"2015年四月新番");
		List<Anime> hotSeasonAnime_201501 = iAnimeService.getHotSeasonAnime(0,10,"2015年一月新番");
		
		List<Anime> hotSeasonAnime_201410 = iAnimeService.getHotSeasonAnime(0,10,"2014年十月新番");
		List<Anime> hotSeasonAnime_201407 = iAnimeService.getHotSeasonAnime(0,10,"2014年七月新番");
		List<Anime> hotSeasonAnime_201404 = iAnimeService.getHotSeasonAnime(0,10,"2014年四月新番");
		List<Anime> hotSeasonAnime_201401 = iAnimeService.getHotSeasonAnime(0,10,"2014年一月新番");
		
		List<Anime> hotSeasonAnime_201310 = iAnimeService.getHotSeasonAnime(0,10,"2013年十月新番");
		List<Anime> hotSeasonAnime_201307 = iAnimeService.getHotSeasonAnime(0,10,"2013年七月新番");
		List<Anime> hotSeasonAnime_201304 = iAnimeService.getHotSeasonAnime(0,10,"2013年四月新番");
		List<Anime> hotSeasonAnime_201301 = iAnimeService.getHotSeasonAnime(0,10,"2013年一月新番");
		
		List<Anime> hotSeasonAnime_201210 = iAnimeService.getHotSeasonAnime(0,10,"2012年十月新番");
		List<Anime> hotSeasonAnime_201207 = iAnimeService.getHotSeasonAnime(0,10,"2012年七月新番");
		List<Anime> hotSeasonAnime_201204 = iAnimeService.getHotSeasonAnime(0,10,"2012年四月新番");
		List<Anime> hotSeasonAnime_201201 = iAnimeService.getHotSeasonAnime(0,10,"2012年一月新番");
		
		
		
		//新番排行榜
		List<Anime> animeRankListBySeason_201507 = iAnimeService.getAnimeRankListBySeason(0, 15, "2015年七月新番");
		List<Anime> animeRankListBySeason_201504 = iAnimeService.getAnimeRankListBySeason(0, 15, "2015年四月新番");
		List<Anime> animeRankListBySeason_201501 = iAnimeService.getAnimeRankListBySeason(0, 15, "2015年一月新番");
		List<Anime> animeRankListBySeason_201410 = iAnimeService.getAnimeRankListBySeason(0, 15, "2014年十月新番");
		List<Anime> animeRankListBySeason_201407 = iAnimeService.getAnimeRankListBySeason(0, 15, "2014年七月新番");
		List<Anime> animeRankListBySeason_201404 = iAnimeService.getAnimeRankListBySeason(0, 15, "2014年四月新番");
		List<Anime> animeRankListBySeason_201401 = iAnimeService.getAnimeRankListBySeason(0, 15, "2014年一月新番");
		List<Anime> animeRankListBySeason_201310 = iAnimeService.getAnimeRankListBySeason(0, 15, "2013年十月新番");
		List<Anime> animeRankListBySeason_201307 = iAnimeService.getAnimeRankListBySeason(0, 15, "2013年七月新番");
		List<Anime> animeRankListBySeason_201304 = iAnimeService.getAnimeRankListBySeason(0, 15, "2013年四月新番");
		List<Anime> animeRankListBySeason_201301 = iAnimeService.getAnimeRankListBySeason(0, 15, "2013年一月新番");
		List<Anime> animeRankListBySeason_201210 = iAnimeService.getAnimeRankListBySeason(0, 15, "2012年十月新番");
		List<Anime> animeRankListBySeason_201207 = iAnimeService.getAnimeRankListBySeason(0, 15, "2012年七月新番");
		List<Anime> animeRankListBySeason_201204 = iAnimeService.getAnimeRankListBySeason(0, 15, "2012年四月新番");
		List<Anime> animeRankListBySeason_201201 = iAnimeService.getAnimeRankListBySeason(0, 15, "2012年一月新番");
		
		//动漫总排行榜
		List<Anime> animeRankAllList = iAnimeService.getAnimeRankAllList(0, 15);
		
		/*==========================================================================================*/
		 
		modelAndView.addObject("allClassify", allClassify);
		
		modelAndView.addObject("newSerialAnime", newSerialAnime);
		modelAndView.addObject("newCompletionAnime", newCompletionAnime);
		modelAndView.addObject("recommendSerialAnime", recommendSerialAnime);
		modelAndView.addObject("recommendCompletionAnime", recommendCompletionAnime);
		
		modelAndView.addObject("detailListForWeek1", detailListForWeek1);
		modelAndView.addObject("detailListForWeek2", detailListForWeek2);
		modelAndView.addObject("detailListForWeek3", detailListForWeek3);
		modelAndView.addObject("detailListForWeek4", detailListForWeek4);
		modelAndView.addObject("detailListForWeek5", detailListForWeek5);
		modelAndView.addObject("detailListForWeek6", detailListForWeek6);
		modelAndView.addObject("detailListForWeek7", detailListForWeek7);
		
		
		modelAndView.addObject("hotSeasonAnime_201507", hotSeasonAnime_201507);
		modelAndView.addObject("hotSeasonAnime_201504", hotSeasonAnime_201504);
		modelAndView.addObject("hotSeasonAnime_201501", hotSeasonAnime_201501);
		modelAndView.addObject("hotSeasonAnime_201410", hotSeasonAnime_201410);
		modelAndView.addObject("hotSeasonAnime_201407", hotSeasonAnime_201407);
		modelAndView.addObject("hotSeasonAnime_201404", hotSeasonAnime_201404);
		modelAndView.addObject("hotSeasonAnime_201401", hotSeasonAnime_201401);
		modelAndView.addObject("hotSeasonAnime_201310", hotSeasonAnime_201310);
		modelAndView.addObject("hotSeasonAnime_201307", hotSeasonAnime_201307);
		modelAndView.addObject("hotSeasonAnime_201304", hotSeasonAnime_201304);
		modelAndView.addObject("hotSeasonAnime_201301", hotSeasonAnime_201301);
		modelAndView.addObject("hotSeasonAnime_201210", hotSeasonAnime_201210);
		modelAndView.addObject("hotSeasonAnime_201207", hotSeasonAnime_201207);
		modelAndView.addObject("hotSeasonAnime_201204", hotSeasonAnime_201204);
		modelAndView.addObject("hotSeasonAnime_201201", hotSeasonAnime_201201);
		
		
		modelAndView.addObject("animeRankListBySeason_201507", animeRankListBySeason_201507);
		modelAndView.addObject("animeRankListBySeason_201504", animeRankListBySeason_201504);
		modelAndView.addObject("animeRankListBySeason_201501", animeRankListBySeason_201501);
		modelAndView.addObject("animeRankListBySeason_201410", animeRankListBySeason_201410);
		modelAndView.addObject("animeRankListBySeason_201407", animeRankListBySeason_201407);
		modelAndView.addObject("animeRankListBySeason_201404", animeRankListBySeason_201404);
		modelAndView.addObject("animeRankListBySeason_201401", animeRankListBySeason_201401);
		modelAndView.addObject("animeRankListBySeason_201310", animeRankListBySeason_201310);
		modelAndView.addObject("animeRankListBySeason_201307", animeRankListBySeason_201307);
		modelAndView.addObject("animeRankListBySeason_201304", animeRankListBySeason_201304);
		modelAndView.addObject("animeRankListBySeason_201301", animeRankListBySeason_201301);
		modelAndView.addObject("animeRankListBySeason_201210", animeRankListBySeason_201210);
		modelAndView.addObject("animeRankListBySeason_201207", animeRankListBySeason_201207);
		modelAndView.addObject("animeRankListBySeason_201204", animeRankListBySeason_201204);
		modelAndView.addObject("animeRankListBySeason_201201", animeRankListBySeason_201201);
		
		
		modelAndView.addObject("newAnime", newAnime);
		modelAndView.addObject("animeRankAllList", animeRankAllList);
		
		return modelAndView;
	}
	
	/**
	 * 跳转到连载动漫
	 * @return
	 */

	@RequestMapping("/serial/{sort}/{pageNum}")
	public ModelAndView serial(@PathVariable int pageNum, @PathVariable String sort){
		System.out.println("serial!");
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("WEB-INF/views/serial");
		
		//标签分类
		List<Tags> allTags = iTagsService.getAllTags();
		modelAndView.addObject("allTags", allTags);
		
		//字母分类
		List<Classify> allClassify = iClassifyService.getAllClassify();
		modelAndView.addObject("allClassify", allClassify);
		
		Page<Anime> AnimeByFilter = null;
		if("new".equals(sort)){
			//最新更新全部
			AnimeByFilter = iAnimeService.getNewAnimeByFilter("连载", pageNum, 20, null, "");
		}else if("hot".equals(sort)){
			//最热播放全部
			AnimeByFilter = iAnimeService.getHotAnimeByFilter("连载", pageNum, 20, null, "");
		}
		modelAndView.addObject("AnimeByFilter", AnimeByFilter);
		
		
		//总榜
		List<Anime> animeRankAllList = iAnimeService.getAnimeRankAllList(0, 30);
		modelAndView.addObject("animeRankAllList", animeRankAllList);
		
		//本周
		List<Anime> animeRankOfWeekList = iAnimeService.getAnimeRankOfWeekList(0, 30);
		modelAndView.addObject("animeRankOfWeekList", animeRankOfWeekList);
		
		//本月
		List<Anime> animeRankOfMonthList = iAnimeService.getAnimeRankOfMonthList(0, 30);
		modelAndView.addObject("animeRankOfMonthList", animeRankOfMonthList);
		
		modelAndView.addObject("sort", sort);
		
		return modelAndView;
	}

	
	/**
	 * 跳转到完结动漫
	 * @return
	 */
	@RequestMapping("/finish/{sort}/{pageNum}")
	public ModelAndView finish(@PathVariable int pageNum, @PathVariable String sort){
		System.out.println("finish!");
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("WEB-INF/views/finish");
		
		//标签分类
		List<Tags> allTags = iTagsService.getAllTags();
		modelAndView.addObject("allTags", allTags);
		
		//字母分类
		List<Classify> allClassify = iClassifyService.getAllClassify();
		modelAndView.addObject("allClassify", allClassify);
		
		Page<Anime> AnimeByFilter = null;
		if("new".equals(sort)){
			//最新更新全部
			AnimeByFilter = iAnimeService.getNewAnimeByFilter("集全", pageNum, 20, null, "");
		}else if("hot".equals(sort)){
			//最热播放全部
			AnimeByFilter = iAnimeService.getHotAnimeByFilter("集全", pageNum, 20, null, "");
		}
		modelAndView.addObject("AnimeByFilter", AnimeByFilter);
		
		//总榜
		List<Anime> animeRankAllList = iAnimeService.getAnimeRankAllList(0, 30);
		modelAndView.addObject("animeRankAllList", animeRankAllList);
		
		//本周
		List<Anime> animeRankOfWeekList = iAnimeService.getAnimeRankOfWeekList(0, 30);
		modelAndView.addObject("animeRankOfWeekList", animeRankOfWeekList);
		
		//本月
		List<Anime> animeRankOfMonthList = iAnimeService.getAnimeRankOfMonthList(0, 30);
		modelAndView.addObject("animeRankOfMonthList", animeRankOfMonthList);
		
		modelAndView.addObject("sort", sort);
		return modelAndView;
	}
	
	/**
	 * 跳转到动漫筛选
	 * @return
	 */
	@RequestMapping("/select/{type}/{sort}/{tag}/{word}/{pageNum}")
	public ModelAndView select(@PathVariable String type, @PathVariable String sort, @PathVariable String tag, @PathVariable Integer word, @PathVariable Integer pageNum){
		System.out.println("select!");
		if("全部".equals(tag)) tag = "";
		if(word == 0) word = null;
		ModelAndView modelAndView = new ModelAndView();
		
		//标签分类
		List<Tags> allTags = iTagsService.getAllTags();
		modelAndView.addObject("allTags", allTags);
		
		//字母分类
		List<Classify> allClassify = iClassifyService.getAllClassify();
		modelAndView.addObject("allClassify", allClassify);
		
		//总榜
		List<Anime> animeRankAllList = iAnimeService.getAnimeRankAllList(0, 30);
		modelAndView.addObject("animeRankAllList", animeRankAllList);
		
		//本周
		List<Anime> animeRankOfWeekList = iAnimeService.getAnimeRankOfWeekList(0, 30);
		modelAndView.addObject("animeRankOfWeekList", animeRankOfWeekList);
		
		//本月
		List<Anime> animeRankOfMonthList = iAnimeService.getAnimeRankOfMonthList(0, 30);
		modelAndView.addObject("animeRankOfMonthList", animeRankOfMonthList);
		
		//排序方式
		modelAndView.addObject("sort", sort);
		
		//标签选择
		modelAndView.addObject("selecttag", tag);
		
		//分类选择
		modelAndView.addObject("selectword", word);
		
		//类型选择
		modelAndView.addObject("selecttype", type);
		
		
		Page<Anime> AnimeByFilter = null;
		//连载和完结
		if("all".equals(type)){
			if("new".equals(sort)){
				//最新更新筛选
				AnimeByFilter = iAnimeService.getNewAnimeByFilter("", pageNum, 20, word, tag);
			}
			else if("hot".equals(sort)){
				//最热更新筛选
				AnimeByFilter = iAnimeService.getHotAnimeByFilter("", pageNum, 20, word, tag);
			}
		}
		//连载
		else if("serial".equals(type)){
			if("new".equals(sort)){
				//最新更新筛选
				AnimeByFilter = iAnimeService.getNewAnimeByFilter("连载", pageNum, 20, word, tag);
			}
			else if("hot".equals(sort)){
				//最热更新筛选
				AnimeByFilter = iAnimeService.getHotAnimeByFilter("连载", pageNum, 20, word, tag);
			}
		}
		//完结
		else if("finish".equals(type)){
			if("new".equals(sort)){
				//最新更新筛选
				AnimeByFilter = iAnimeService.getNewAnimeByFilter("集全", pageNum, 20, word, tag);
			}
			else if("hot".equals(sort)){
				//最热更新筛选
				AnimeByFilter = iAnimeService.getHotAnimeByFilter("集全", pageNum, 20, word, tag);
			}
		}
		modelAndView.addObject("AnimeByFilter", AnimeByFilter);
		modelAndView.setViewName("WEB-INF/views/select");
		
		return modelAndView;
	}
	
	
	/**
	 * 跳转到detail页面
	 * @return
	 */
	@RequestMapping("/detail/{id}")
	public ModelAndView detail(@PathVariable int id){
		System.out.println("detail!");
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("WEB-INF/views/detail");
		
		//字母分类
		List<Classify> allClassify = iClassifyService.getAllClassify();
		modelAndView.addObject("allClassify", allClassify);
		
		DetailWithBLOBs detail = iDetailService.getDetailByAnimeId(id);
		String rate = detail.getRate();
		String[] rates = rate.split("-");
		
		
		modelAndView.addObject("detail", detail);
		modelAndView.addObject("rates", rates);
		return modelAndView;
	}
	
	/**
	 * 跳转到搜索页面
	 * @param search_text
	 * @return
	 */
	@RequestMapping("/search/{search_text}/{pageNum}")
	public ModelAndView search(@PathVariable String search_text,@PathVariable int pageNum){
		System.out.println("search!");
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("WEB-INF/views/search");
		
		//字母分类
		List<Classify> allClassify = iClassifyService.getAllClassify();
		modelAndView.addObject("allClassify", allClassify);
		
		//搜索内容
		Page<Anime> animeBySearch = iAnimeService.getAnimeBySearch(pageNum, 25, search_text);
		modelAndView.addObject("animeBySearch", animeBySearch);
		
	
		
		//总榜
		List<Anime> animeRankAllList = iAnimeService.getAnimeRankAllList(0, 30);
		modelAndView.addObject("animeRankAllList", animeRankAllList);
		
		//本周
		List<Anime> animeRankOfWeekList = iAnimeService.getAnimeRankOfWeekList(0, 30);
		modelAndView.addObject("animeRankOfWeekList", animeRankOfWeekList);
		
		//本月
		List<Anime> animeRankOfMonthList = iAnimeService.getAnimeRankOfMonthList(0, 30);
		modelAndView.addObject("animeRankOfMonthList", animeRankOfMonthList);
		
		//搜索关键字
		modelAndView.addObject("search_text", search_text);
		
		return modelAndView;
	}


}
